// pages/teamEntry/teamEntry.js
var that,
  app = getApp(),
  api = new (require('../../utils/api.js')),
  util = new (require('../../utils/util.js')),
  headimg = api.getimgurl();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    worktypelist: [[], [{ name: '全部', id: 0 }]],
    primarytypeList: [],//一级工种分类列表
    subtypeList: [],//二级工种分类列表
    worktype: 0,//一级工种id
    worksubtype: 0,//二级工种id
    worktypename: '',//一级工种
    worksubtypename: '',//二级工种
    name: '',
    province: '',
    city: '',
    district: '',
    bank: '',//开户行
    bankcard: '',//银行卡号
    identitynum: '',//身份证件
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    that.getdata()
  },
  getdata() {
    wx.showLoading({
      title: '加载中...',
    })
    let { primarytypeList, subtypeList, worktypelist, provincelist, citylist, range, recruitlist } = that.data, newslist = [{ name: '全部', id: 0 }]
    api.getsubMechanicstypelist(res => {
      wx.hideLoading()
      if (res.data.issuccess == 1) {
        primarytypeList = newslist.concat(res.data.list)
        subtypeList = res.data.sublist
        worktypelist[0] = primarytypeList
        that.setData({ worktypelist, subtypeList, primarytypeList })
      }
    })
  },
  //工种
  changeType(e) {
    console.log(e, 'eeeeeee');
    let { worktypelist, worktype, worksubtype, worktypename, worksubtypename } = that.data, { value } = e.detail
    worktype = worktypelist[0][value[0]].id
    worksubtype = worktypelist[1][value[1]].id
    worktypename = worktypelist[0][value[0]].name
    worksubtypename = worktypelist[1][value[1]].name
    that.setData({ worktypename, worktype, worksubtype, worksubtypename })
  },
  TypeColumnChange(e) {
    let { column, value } = e.detail, { worktypelist, subtypeList, primarytypeList } = that.data, list = [], newslist = [{ name: '全部', id: 0 }]
    if (column == 0) {
      subtypeList.forEach(x => {
        if (primarytypeList[value].id == x.cid) {
          list.push(x)
        }
      })
      list = newslist.concat(list)
      worktypelist[1] = list
    }
    that.setData({ worktypelist })
  },
  changeRegion(e) {
    that.setData({ province: e.detail.value[0], city: e.detail.value[1], district: e.detail.value[2] })
  },
  settled() {
    let { worktypename, province, city, district, identitynum, bankcard, bank, worksubtype, worktype, name } = that.data, err = ''
    if (province == '') err = '请选择地区'
    if (worktypename == '') err = '请选择工种类型'
    if (name == '') err = '请输入班组姓名'
    if (err.length > 0) {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      return
    }
    api.addSettledteam({  province, city, district, identitynum, bankcard, bank, worksubtype, worktype, name },res => {
      if (res.data.issuccess == 1) {
        wx.showModal({
          title: '提示',
          content: '提交成功',
          success(res) {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      } else {
        wx.showModal({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  inputtap(e){
    let {prop} = e.currentTarget.dataset
    that.setData({[prop]:e.detail.value})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})